﻿using ClassLibrary;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Forms
{
    public partial class MySupermarket : UIForm
    {
        ItemService itemService = new ItemService();
        MarketService marketService = new MarketService();
        User user;
        Market myMarket;
        List<Item> items;
        public MySupermarket(User user)
        {
            SetStyle(
                ControlStyles.UserPaint//使用自定义的绘制方式
                | ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
                | ControlStyles.DoubleBuffer// 双缓冲
                | ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
                | ControlStyles.AllPaintingInWmPaint//禁止擦除背景.则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
                | ControlStyles.EnableNotifyMessage// 
                                                   // Enable the OnNotifyMessage event so we get a chance to filter out 
                                                   // Windows messages before they get to the form's WndProc
                | ControlStyles.OptimizedDoubleBuffer//则控件将首先绘制到缓冲区而不是直接绘制到屏幕，这可以减少闪烁
                , true);
            InitializeComponent();
            this.user = user;
            List<Market> myMarkets = marketService.GetMarketByUserID(user.UserID);
            if (myMarkets.Count != 0)
            {
                myMarket = myMarkets[0];
                uiLabel1.Visible = false;
                items = itemService.GetItemByMarketID(myMarket.MarketID);
                ListItems(items);
            }
            else
            {
                addButton.Enabled = false;
                soldButton.Enabled = false;
            }
            Color userColor = MainForm.MainForm.userColor;
            UIStyles.InitColorful(userColor, Color.White);
        }

        public void ListItems(List<Item> items)//列表化商品
        {
            panel.Controls.Clear();
            panel.AutoScroll = true; // 启用自动滚动条
            int boxHeight = 160;
            int i = 0;
            foreach (Item item in items)//对于每个发送者有一个消息框
            {
                Itembox box = new Itembox(user, item);
                box.FlashParentForm += flashForm;
                box.Top = i * boxHeight; // 设置用户控件的位置
                i++;
                panel.Controls.Add(box); // 将用户控件添加到 Panel 中
            }
        }

        private void Search_Click(object sender, EventArgs e)
        {
            List<Item> tempItems = new List<Item>();
            if (KeyTextBox.Text == "" || KeyTextBox.Text == null)
            {
                ListItems(items);
            }
            else
            {
                string key = KeyTextBox.Text;
                tempItems = itemService.GetItemByMarketIDandKey(myMarket.MarketID, key);
                ListItems(tempItems);
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            AddItem addItem = new AddItem(user, myMarket.MarketID);
            addItem.ShowDialog();
            items = itemService.GetItemByMarketID(myMarket.MarketID);
            ListItems(items);
        }

        private void setButton_Click(object sender, EventArgs e)
        {
            MarketSet marketSet = new MarketSet(user, myMarket);
            marketSet.ShowDialog();
            if (myMarket != null)
            {

            }
            else
            {
                List<Market> myMarkets = marketService.GetMarketByUserID(user.UserID);
                if (myMarkets.Count != 0)
                {
                    myMarket = myMarkets[0];
                    uiLabel1.Visible = false;
                    addButton.Enabled = true;
                    soldButton.Enabled = true;
                }
            }
        }

        private void flashForm()
        {
            items = itemService.GetItemByMarketID(myMarket.MarketID);
            ListItems(items);
        }

        private void soldButton_Click(object sender, EventArgs e)
        {
            SoldGoods soldGoods = new SoldGoods(user);
            soldGoods.ShowDialog();
        }
    }
}
